home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / Archivers / ZLib / minigzip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-20  |  5.6 KB  |  238 lines

  1. /* minigzip.c -- simulate gzip using the zlib compression library
  2.  * Copyright (C) 1995-1996 Jean-loup Gailly.
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6. /*
  7.  * minigzip is a minimal implementation of the gzip utility. This is
  8.  * only an example of using zlib and isn't meant to replace the
  9.  * full-featured gzip. No attempt is made to deal with file systems
  10.  * limiting names to 14 or 8+3 characters, etc... Error checking is
  11.  * very limited. So use minigzip only for testing; use gzip for the
  12.  * real thing. On MSDOS, use only on file names without extension
  13.  * or in pipe mode.
  14.  */
  15.  
  16. /* $Id: minigzip.c,v 1.10 1996/07/24 13:41:04 me Exp $ */
  17.  
  18. #include <stdio.h>
  19. #include "zlib.h"
  20.  
  21. #ifdef STDC
  22. #  include <string.h>
  23. #  include <stdlib.h>
  24. #else
  25.    extern void exit  OF((int));
  26. #endif
  27.  
  28.  
  29. #if defined(MSDOS) || defined(OS2) || defined(WIN32)
  30. #  include <fcntl.h>
  31. #  include <io.h>
  32. #  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
  33. #else
  34. #  define SET_BINARY_MODE(file)
  35. #endif
  36.  
  37. #ifdef VMS
  38. #  define unlink delete
  39. #  define GZ_SUFFIX "-gz"
  40. #endif
  41. #ifdef RISCOS
  42. #  define unlink remove
  43. #  define GZ_SUFFIX "-gz"
  44. #  define fileno(file) file->__file
  45. #endif
  46.  
  47. #ifndef GZ_SUFFIX
  48. #  define GZ_SUFFIX ".gz"
  49. #endif
  50. #define SUFFIX_LEN sizeof(GZ_SUFFIX)
  51.  
  52. extern int unlink OF((const char *));
  53.  
  54. #define BUFLEN 4096
  55. #define MAX_NAME_LEN 1024
  56.  
  57. #define local static
  58. /* For MSDOS and other systems with limitation on stack size. For Unix,
  59.     #define local
  60.    works also.
  61.  */
  62.  
  63. char *prog;
  64.  
  65. void error           OF((const char *msg));
  66. void gz_compress     OF((FILE   *in, gzFile out));
  67. void gz_uncompress   OF((gzFile in, FILE   *out));
  68. void file_compress   OF((char  *file));
  69. void file_uncompress OF((char  *file));
  70. int  main            OF((int argc, char *argv[]));
  71.  
  72. /* ===========================================================================
  73.  * Display error message and exit
  74.  */
  75. void error(const char *msg)
  76. {
  77.     fprintf(stderr, "%s: %s\n", prog, msg);
  78.     exit(1);
  79. }
  80.  
  81. /* ===========================================================================
  82.  * Compress input to output then close both files.
  83.  */
  84. void gz_compress(FILE *in, gzFile out)
  85. {
  86.     local char buf[BUFLEN];
  87.     int len;
  88.     int err;
  89.  
  90.     for (;;) {
  91.         len = fread(buf, 1, sizeof(buf), in);
  92.         if (ferror(in)) {
  93.             perror("fread");
  94.             exit(1);
  95.         }
  96.         if (len == 0) break;
  97.  
  98.         if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
  99.     }
  100.     fclose(in);
  101.     if (gzclose(out) != Z_OK) error("failed gzclose");
  102. }
  103.  
  104. /* ===========================================================================
  105.  * Uncompress input to output then close both files.
  106.  */
  107. void gz_uncompress(gzFile in, FILE *out)
  108. {
  109.     local char buf[BUFLEN];
  110.     int len;
  111.     int err;
  112.  
  113.     for (;;) {
  114.         len = gzread(in, buf, sizeof(buf));
  115.         if (len < 0) error (gzerror(in, &err));
  116.         if (len == 0) break;
  117.  
  118.         if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
  119.             error("failed fwrite");
  120.         }
  121.     }
  122.     if (fclose(out)) error("failed fclose");
  123.  
  124.     if (gzclose(in) != Z_OK) error("failed gzclose");
  125. }
  126.  
  127.  
  128. /* ===========================================================================
  129.  * Compress the given file: create a corresponding .gz file and remove the
  130.  * original.
  131.  */
  132. void file_compress(char *file)
  133. {
  134.     local char outfile[MAX_NAME_LEN];
  135.     FILE  *in;
  136.     gzFile out;
  137.  
  138.     strcpy(outfile, file);
  139.     strcat(outfile, GZ_SUFFIX);
  140.  
  141.     in = fopen(file, "rb");
  142.     if (in == NULL) {
  143.         perror(file);
  144.         exit(1);
  145.     }
  146.     out = gzopen(outfile, "wb"); /* use "wb9" for maximal compression */
  147.     if (out == NULL) {
  148.         fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
  149.         exit(1);
  150.     }
  151.     gz_compress(in, out);
  152.  
  153.     unlink(file);
  154. }
  155.  
  156.  
  157. /* ===========================================================================
  158.  * Uncompress the given file and remove the original.
  159.  */
  160. void file_uncompress(char *file)
  161. {
  162.     local char buf[MAX_NAME_LEN];
  163.     char *infile, *outfile;
  164.     FILE  *out;
  165.     gzFile in;
  166.     int len = strlen(file);
  167.  
  168.     strcpy(buf, file);
  169.  
  170.     if (len > SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
  171.         infile = file;
  172.         outfile = buf;
  173.         outfile[len-3] = '\0';
  174.     } else {
  175.         outfile = file;
  176.         infile = buf;
  177.         strcat(infile, GZ_SUFFIX);
  178.     }
  179.     in = gzopen(infile, "rb");
  180.     if (in == NULL) {
  181.         fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
  182.         exit(1);
  183.     }
  184.     out = fopen(outfile, "wb");
  185.     if (out == NULL) {
  186.         perror(file);
  187.         exit(1);
  188.     }
  189.  
  190.     gz_uncompress(in, out);
  191.  
  192.     unlink(infile);
  193. }
  194.  
  195.  
  196. /* ===========================================================================
  197.  * Usage:  minigzip [-d] [files...]
  198.  */
  199.  
  200. int main(int argc, char *argv[])
  201. {
  202.     int uncompr = 0;
  203.     gzFile file;
  204.  
  205.     prog = argv[0];
  206.     argc--, argv++;
  207.  
  208.     if (argc > 0) {
  209.         uncompr = (strcmp(*argv, "-d") == 0);
  210.         if (uncompr) {
  211.             argc--, argv++;
  212.         }
  213.     }
  214.     if (argc == 0) {
  215.         SET_BINARY_MODE(stdin);
  216.         SET_BINARY_MODE(stdout);
  217.         if (uncompr) {
  218.             file = gzdopen(fileno(stdin), "rb");
  219.             if (file == NULL) error("can't gzdopen stdin");
  220.             gz_uncompress(file, stdout);
  221.         } else {
  222.             file = gzdopen(fileno(stdout), "wb"); /* "wb9" for max compr. */
  223.             if (file == NULL) error("can't gzdopen stdout");
  224.             gz_compress(stdin, file);
  225.         }
  226.     } else {
  227.         do {
  228.             if (uncompr) {
  229.                 file_uncompress(*argv);
  230.             } else {
  231.                 file_compress(*argv);
  232.             }
  233.         } while (argv++, --argc);
  234.     }
  235.     exit(0);
  236.     return 0; /* to avoid warning */
  237. }
  238.